<?php
/**
 * Created by PhpStorm.
 * User: liyiming
 * Date: 2020/7/20
 * Time: 16:58
 */

interface ExpressLocation{
    public function GetLocation($data,$mod);
}


class Express_SF implements ExpressLocation{
    public function GetLocation($data,$mod){
        $sfe = new SFExpressClass($data['debug'],$data['sf_dev_key'],$data['sf_dev_id'],$data['sf_user_name']);
        $r = $sfe->riderlatestposition($data['express_order_no']);
        $location['errorcode'] = 0;
        if($r && $r['error_code'] != 0)
            $location['errorcode'] = 1;
        $location['exp_lat'] = $r['result']['rider_lat'];
        $location['exp_lng'] = $r['result']['rider_lng'];
        $location['msg'] = $r['error_msg'];
        return $location;
    }
}


class Express_DD implements ExpressLocation{
    public function GetLocation($data,$mod){
        $dde = new DDExpressClass($data['debug'],$data['dd_source_id'],$data['dd_app_key'],$data['dd_app_secret']);
        $parameter = [
            'order_id' => $data['express_order_no'],
        ];
        $r = $dde->query($parameter);
        $location['errorcode'] = 0;
        if($r['status'] != 'success' || $r['code'] != 0)
            $location['errorcode'] = 1;
        $location['exp_lat'] = $r['result']['transporterLat'];
        $location['exp_lng'] = $r['result']['transporterLng'];
        $location['msg'] = $r['msg'];
        return $location;
    }
}


class Express_SS implements ExpressLocation{
    public function GetLocation($data,$mod){
        $sse = new SSExpressClass($data['debug'],$data['ss_clientId'],$data['ss_clientSecret'],$data['ss_shopId']);
        $r = $sse->courier(['issOrderNo'=>$data['express_order_no'],'orderNo'=>$data['order_no']]);
        $location['errorcode'] = 0;
        if($r['status'] != 200)
            $location['errorcode'] = 1;
        $location['exp_lat'] = $r['data']['latitude'];
        $location['exp_lng'] = $r['data']['longitude'];
        $location['msg'] = $r['msg'];
        return $location;

    }
}

class Express_JRD implements ExpressLocation{
    public function GetLocation($data,$mod){
        $JRD = new JRDExpressClass($data['debug'],$data['jrd_sign']);
        $r = $JRD->orderrider($data['express_order_no']);
        $location['errorcode'] = 0;
        if($r['error_code'] != 0)
            $location['errorcode'] = 1;
        $location['exp_lat'] = $r['result']['latitude'];
        $location['exp_lng'] = $r['result']['longitude'];
        $location['msg'] = $r['error_msg'];
        return $location;
    }
}


class Express_UU implements ExpressLocation{
    public function GetLocation($data,$mod){
        $uue = new UUExpressClass($data['debug'],$data['uu_app_id'],$data['uu_app_key'],$data['uu_open_id']);
        $r = $uue->getorderdetail($data['express_order_no']);
        $location['errorcode'] = 0;
        if($r['return_code'] != 'ok'){
            $location['errorcode'] = 1;
        }
        $location['msg'] = $r['return_msg'];
        if($r['driver_lastloc'] && $r['driver_lastloc'] != 'null' && strlen($r['driver_lastloc']) > 0){
            $lastloc = explode(',',$r['driver_lastloc']);
            $location['exp_lat'] = $lastloc[0];
            $location['exp_lng'] = $lastloc[1];
        }else{
            $location['errorcode'] = 1;
            $location['msg'] = '骑手定位经纬度异常';
        }
        return $location;
    }
}



class Express_SLW implements ExpressLocation{
    public function GetLocation($data,$mod){
        $row = new SLWTokenClass($data['debug'],$mod);
        $token = $row->token();
        $SLW = new SLWExpressClass($data['debug'],$data['user365Id']);
        $data['token'] = $token;
        $data['orderId'] = $data['express_order_no'];
        $r = $SLW->orderLocation($data);
        $location['exp_lat'] = $r['data']['orderLat'];
        $location['exp_lng'] = $r['data']['orderLon'];
        $location['errorcode'] = $r['errorcode'];
        $location['msg'] = $r['msg'];
        return $location;
    }
}


class GetExpressLocation{
    protected $express_class = null;
    public function get_result($data,$mod){
        $this->express_class = 'Express_' . strtoupper($data['express_type']);
        $cls = new $this->express_class;
        return $cls->GetLocation($data,$mod);
    }
}

